package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.UserLike;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 左滑不喜欢右滑喜欢
     * @param userId
     * @param likeUserId
     * @param isLike
     * @return
     */
    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //查询
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //判断是否为空
            if (userLike == null) {
                //如果为空就创建
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            } else {
                //如果不为空就更新
                Update update = Update.update("isLike", isLike)
                        .set("updated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * UserHolder.getUserId()
     * @param userId
     * @return
     */
    @Override
    public Map findById(Long userId) {
        //喜欢
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        long loveCount = mongoTemplate.count(query1, UserLike.class);

        //粉丝
        Query query2 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        long fanCount = mongoTemplate.count(query2, UserLike.class);

        //互相喜欢
        //查询当前id，构造我喜欢别人的条件。
        Query query3 = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query3, UserLike.class);
        //查询我喜欢别人的所有id
        List<Long> likeUserId = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);

        long eachLoveCount=0;
        //遍历
        for (Long aLong : likeUserId) {
            //构造别人是否喜欢我的条件
            Query querys = Query.query(Criteria.where("userId").is(aLong).and("isLike").is(true));
            UserLike like = mongoTemplate.findOne(querys, UserLike.class);
            //双向喜欢一旦成立，判断是否有数据，如果有数据就 累加+1
            if (like!=null){
                eachLoveCount++;
            }
        }
        Map<String,Long> map=new HashMap<>();
        map.put("loveCount",loveCount);
        map.put("fanCount",fanCount);
        map.put("eachLoveCount",eachLoveCount);
        return map;
    }
}
